package org.wzl.todolisthelper.utils;

import org.wzl.todolisthelper.vo.TimeLeftVO;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public class TimeUtils {

    public static String getTimeRange(LocalDateTime startTime, LocalDateTime endTime) {
        StringBuilder ans = new StringBuilder();
        //判断是不是今天
        if (isToday(startTime)) {
            StringBuilder am = new StringBuilder();
            StringBuilder pm = new StringBuilder();
            am.append(startTime.format(DateTimeFormatter.ofPattern("HH:mm")));
            if (isAM(startTime)) {
                am.append("上午");
            } else {
                am.append("下午");
            }

            if (isToday(endTime)) {
                pm.append(endTime.format(DateTimeFormatter.ofPattern("HH:mm")));
                if (isPM(endTime)) {
                    pm.append("下午");
                } else {
                    pm.append("上午");
                }
            } else {
                pm.append(endTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm")));
            }

            ans.append(am).append(" ").append(pm);
        } else {
            ans.append(startTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm")));
        }
        return ans.toString();
    }

    public static boolean isToday(LocalDateTime time) {
        return LocalDateTime.now().getDayOfMonth() == time.getDayOfMonth()
                && LocalDateTime.now().getMonthValue() == time.getMonthValue()
            && LocalDateTime.now().getYear() == time.getYear();
    }

    public static boolean isAM(LocalDateTime time) {
        return time.getHour() < 12;
    }

    public static boolean isPM(LocalDateTime time) {
        return time.getHour() >= 12;
    }

    /**
     * 获取时间距离现在还剩多少天。
     * 如果不是本年内，则返回具体日期
     * 如果是今年内，则返回今年xx月xx号
     * 如果是本月内，则返回本月xx号
     * 如果是今天内，则返回剩余时间
     * 如果是一个小时内，则返回多少分钟
     * 如果是一个小时外，则返回已逾期的天数
     * @param taskEndDate   结束日期
     * @return              string, 剩余时间
     */
    public static TimeLeftVO getTimeLeft(LocalDateTime taskEndDate) {
        LocalDateTime now = LocalDateTime.now();
        TimeLeftVO timeLeftVO = new TimeLeftVO();
        //之前
        if (taskEndDate.isAfter(now)) {
            timeLeftVO.setOverTime(TimeLeftVO.OverTimeEnum.UN_OVER_TIME.getCode());
            if (taskEndDate.getYear() != now.getYear()) {
                timeLeftVO.setDetail(taskEndDate.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm")));
            } else if (taskEndDate.getMonth() != now.getMonth()) {
                timeLeftVO.setDetail(taskEndDate.format(DateTimeFormatter.ofPattern("MM-dd HH:mm")));
            } else if (taskEndDate.getDayOfMonth() != now.getDayOfMonth()) {
                timeLeftVO.setDetail("本月" + taskEndDate.getDayOfMonth() + "号");
            } else if (taskEndDate.getHour() != now.getHour()) {
                timeLeftVO.setDetail("今天" + taskEndDate.getHour() + "点");
            } else {
                timeLeftVO.setDetail("小于1小时");
            }
        } else {
            //之后
            timeLeftVO.setOverTime(TimeLeftVO.OverTimeEnum.OVER_TIME.getCode());

            if (taskEndDate.getYear() != now.getYear()) {
                timeLeftVO.setDetail("超过一年");
            } else if (taskEndDate.getMonth() != now.getMonth()) {
                timeLeftVO.setDetail("超过1个月");
            } else if (taskEndDate.getDayOfMonth() != now.getDayOfMonth()) {
                timeLeftVO.setDetail("超过" + (now.getDayOfMonth() - taskEndDate.getDayOfMonth()) + "天");
            } else if (taskEndDate.getHour() != now.getHour()) {
                timeLeftVO.setDetail("超过" + (now.getHour() - taskEndDate.getHour()) + "小时");
            } else {
                timeLeftVO.setDetail("超过" + (now.getMinute() - taskEndDate.getMinute()) + "分钟");
            }
        }
        return timeLeftVO;
    }

}
